import { Injectable } from '@nestjs/common';
import { JwtService as NestJwtService } from '@nestjs/jwt';
import { RedisService } from '../redis/redis.service';

@Injectable()
export class JwtService {
  constructor(
    private readonly nestJwtService: NestJwtService,
    private readonly redisCacheService: RedisService,
  ) {}

  async createToken(payload: any): Promise<string> {
    const token = this.nestJwtService.sign(payload);
    await this.redisCacheService.setUserToken(payload.sub, token);
    return token;
  }

  verifyToken(token: string): Promise<any> {
    return this.nestJwtService.verify(token);
  }

  async removeToken(userId: string): Promise<void> {
    await this.redisCacheService.deleteUserToken(userId);
  }
}
